# 准备数据
import numpy as np

x_train = np.array([1.7, 1.5, 1.3, 5, 1.3, 2.2])
y_train = np.array([368, 340, 376, 954, 331, 556])
x_train = x_train.reshape(-1, 1)

#训练模型
from sklearn.linear_model import LinearRegression
lr = LinearRegression()
lr.fit(x_train, y_train)
#打印参数
print("the trained model is :y={}x + {}".format(lr.coef_, lr.intercept_))

y_train_pred_lr = lr.predict(x_train)


# 模型可视化
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(x_train, y_train)

#画函数线
plt.plot(x_train, y_train_pred_lr, 'g-', label='LR pre')

plt.show()